(function(){

YAHOO.pendular.RepositoryTagCloud = function(config){
    if (!config) {
        config = {};
    }
    YAHOO.lang.augmentObject(config, {
        maxFontSize: 30,
        minFontSize: 0,
        target: "pendular-tagcloud-body",
        repository: YAHOO.pendular.app.repository
    });
    this.config = config;
    this.onWordClicked = new YAHOO.util.CustomEvent("wordClicked");
};


YAHOO.extend(YAHOO.pendular.RepositoryTagCloud, Object, {
    applyStyle: function(keywords){
        var config = this.config,
            minCount = keywords.minCount,
            range = keywords.maxCount - minCount,
            maxFontSize = config.maxFontSize,
            minFontSize = 3, //config.minFontSize,
            factor = (maxFontSize - minFontSize)/range
        ;
        YAHOO.util.Dom.batch(
            YAHOO.util.Dom.getChildren(config.target),
            function(e, o){
                var style,
                    word = e.getAttribute("data-keyword"),
                    keyword = o.keywords.keywords[word];
                if (keyword) {
                    fontSize = o.factor * (keyword.count - o.keywords.minCount) + o.minFontSize;
                    style = "font-size: " + fontSize + "pt"
                }
                else {
                    style = "display:none";
                }
                e.setAttribute("style", style);
            },
            {
                keywords: keywords,
                minFontSize: minFontSize,
                factor: (maxFontSize - minFontSize)/range
            }
        );
    },
    getWordId: function(key){
        return "pendulum-keyword-" + key;
    },
    getWordHTML: function(word){
        return "<a " +
                " id=\"" + this.getWordId(word.key) +"\""+
                " data-keyword=\"" + word.key +"\""+
                " title=\"" + word.word + "\"" +
                " onclick=\"" +
                    "YAHOO.pendular.app.repositoryTagCloud.onWordClicked.fire(this);" +
                    "return false;" +
                "\"" + 
                " href=\"" +
                    "?query=" + encodeURIComponent(word.word) + 
                    "\"" +
                ">" + word.word + "</a> ";
    },
    render: function(){
        var config = this.config,
            target = YAHOO.util.Dom.get(config.target),
            repository = config.repository,
            keywords = repository.keywords,
            words, numWords, word,
            html = ""
        ;
        if (!target) {
            throw "Invalid Target for tagcloud";
        }
        if (!repository) {
            throw "Invalid Repository for tagcloud";
        }
        words = keywords.getArray(true);
        numWords = words.length;
        for (var i=0; i<numWords; i++) {
            word = words[i];
            html += this.getWordHTML(word);
        }
        target.innerHTML = html;
        this.applyDefaultStyles();
    },
    applyDefaultStyles: function(){
        var config = this.config,
            repository = config.repository,
            keywords = repository.keywords
        ;
        this.applyStyle(keywords);
    }
});


})();
